<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\RedRecordModel;

/**
 * 红包记录
 * Class Red
 * @package Controller
 */
class RedRecord extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增记录
     * @param array $_data 数据
     * @param int $_uid 用户id
     * @return mixed
     */
    public function add(Array $_data, $_uid = 0)
    {
        $_data["uid"] = $_uid ? $_uid : $this->_uid;
        $_data["uniacid"] = $this->_uniacid;
        return RedRecordModel::createId($_data);
    }

    /**
     * 获取一条数据
     * @param int $_red_id 红包id
     * @return RedRecordModel
     */
    public function take($_red_id)
    {
        return RedRecordModel::where("red_id", $_red_id)
            ->where("uid", $this->_uid)
            ->where("pid", 0)
            ->field(array("money", "time"))
            ->noGetAttr(false)
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 获取一条开红包数据
     * @param $_red_id
     * @return RedRecordModel
     */
    public function find($_red_id)
    {
        return RedRecordModel::where("red_id", $_red_id)
            ->where("uid", $this->_uid)
            ->where("pid", 0)
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 获取一条分享红包开包奖励记录
     * @param $_pid
     * @return RedRecordModel
     */
    public function findShare($_pid)
    {
        return RedRecordModel::where("pid", $_pid)
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 获取一个红包的开包记录
     * @param integer $_red_id 红包id
     * @param array $_limit 请求下标
     * @return mixed
     */
    public function getList($_red_id, Array $_limit)
    {
        $_list = RedRecordModel::where("red_id", $_red_id)
            ->where("pid", 0)
            ->field(array("id", "uid", "money", "time"))
            ->limit($_limit[0], $_limit[1])
            ->cache(!KUYUAN_DEBUG)
            ->order("uid={$this->_uid}","desc")
            ->order("id","desc")
            ->select();
        if ($_list && count($_list) > 0) {
            foreach ($_list as $_k => $_v) {
                $_list[$_k]["share_award"] = RedRecordModel::where("pid", $_v["id"])
                    ->field(array("uid", "money"))
                    ->find();
            }
        }
        return $_list;
    }

    /**
     * 获取一个用户已开红包总额
     * @param $_uid
     * @return mixed
     */
    public function getUserMoney($_uid = 0)
    {
        if ($_uid == 0) $_uid = $this->_uid;
        return RedRecordModel::where("uid", $_uid)
            ->cache(!KUYUAN_DEBUG)
            ->sum("money");
    }

    /**
     * 获取一个用户的收入记录
     */
    public function getUserIncome(Array $_limit,$_uid = 0)
    {
        if ($_uid == 0) $_uid = $this->_uid;
        return RedRecordModel::where("uniacid", $this->_uniacid)
            ->where("uid",$_uid)
            ->field(array("id","money","pid","time"))
            ->limit($_limit[0],$_limit[1])
            ->order("time","desc")
            ->select();
    }
}